<?php
namespace Apps\BoxSns\Model;

use Think\Model;

class SnsTopicModel extends \BaseModel {
	public function load($id = "") {
		$data = $this->where(array('id' => $id))->find();
		if (!$data) {
			return false;
		}
		$data['title'] = str_replace("<{ImgDomain}>", ImgDomain, $data['title']);
		$data['content'] = str_replace("<{ImgDomain}>", ImgDomain, $data['content']);
		$data['hit_count']=intval($data['hit_count']*1.5);
		//$data['content']=relpace_emotion($data['content']);
		$data['title'] = relpace_emotion($data['title']);
		$type = $data['type'];
		/*********************数据处理*******/
		$data['title'] = nl2br($data['title']); //标题换行符
		if (!$data['title']) {
			$data['title'] = cutstr(strip_tags($data['content']), "70");
		}

		$data['has_good'] = BoxModel("sns_topic_goods")->where(array('topic_id' => $id, 'user_id' => $this->user_id))->find();
		$data['comment_count'] = BoxModel("sns_comment")->where("pid='$id'")->count();
		$data['goods_count'] = BoxModel("sns_topic_goods")->where("topic_id='$id'")->count();
		$data['time_tran'] = time_tran($data['dateline']);
		$data['imgList'] = getContentPic($data['content']); //内容中的图片
		$data['imgList'] = $data['imgList'] ? $data['imgList'] : array();
		$data['imgCount'] = is_array($data['imgList']) ? count($data['imgList']) : 0;
		$data['coverImage'] = $data['imgList'][0];
		$data['user'] = BoxModel("system_member")->load($data['user_id']);
		hook("topic_load", $data);
		hook("topic_load_" . $data['type'], $data);
		return $data;
	}

	/******设置为best***/
	public function best($id) {
		return $this->where("id = '$id'")->save(array('is_best' => 1));
	}

	/*****浏览量加1*/
	public function hit_count($id) {
		$key = session_id() . "_" . $id;
		if (!S($key)) {
			S($key, 1);
			return $this->where("id = '$id'")->setInc("hit_count");
		}

	}

	/*******发布帖子***/
	public function create_topic($data) {
		$row['postip'] = getIp();
		$pid = $data['pid'];
		$type = $data['type'];
		hook("topic_before_save", $data);
		hook("topic_before_save_" . $type, $data);
		$row = $this->create($data);
		if ($id = $row['id']) {
			$row['lastupdate'] = time();
			$r = $this->where(array('id' => $id))->save($row);
			if ($r) {
				hook("topic_update", $id);
				hook("topic_update_" . $type, $id);
			}
		} else {
			hook("topic_before_insert", $data);
			hook("topic_before_insert_" . $type, $data);
			$row['dateline'] = time();
			$row['lastupdate'] = time();
			$id = $this->add($row);
			// dump($id);
			if ($id) {
				$data['id'] = $id;
				hook("topic_insert", $data);
				hook("topic_insert_" . $type, $data);
			}
		}

		if ($id) {
			$data['id'] = $id;
			hook("topic_after_save", $data);
			hook("topic_after_save_" . $type, $data);
		}
		S("user_statistics_" . $data['user_id'], null);

		return $id;
	}

	/********点赞数，或回复数加1*****/
	public function statistics($topic_id = "", $field = "comment_count", $set = "setInc") {
		$data = $this->load($topic_id);
		if ($data) {
			$type = $data['type'];
			$model = BoxModel("sns_topic");
			$model->where(array('id' => $topic_id))->$set($field);
			$model->where(array('id' => $topic_id))->save(array('lastupdate' => time()));
		}
	}

	/*****删除一个帖子*****/
	public function del($id = "") {
		$data = $this->load($id);
		$type = $data['type'];
		hook("topic_before_del", $id);
		hook("topic_before_del_" . $type, $id);
		$r = BoxModel("sns_topic")->where(array('id' => $id))->save(array('id' => $id, 'is_show' => 0));
		S("user_statistics_" . $data['user_id'], null); //删除用户的汇总缓存
		if ($r) {
			hook("topic_after_del", $id);
			hook("topic_after_del_" . $type, $id);
		}
	}

}
